<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="所属分销商" prop="dtName">
        <el-popover ref="dtListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="dtList"
            :props="dtListTreeProps"
            node-key="id"
            ref="dtTree"
            @current-change="dtListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.dtName"
          v-popover:dtListPopover
          :readonly="true"
          placeholder="点击选择所属分销商"
        ></el-input>
      </el-form-item>
      <el-form-item label="价格区间始" prop="changePriceStart">
        <el-input v-model="dataForm.changePriceStart" placeholder="价格区间始"></el-input>
      </el-form-item>
      <el-form-item label="价格区间止" prop="changePriceEnd">
        <el-input v-model="dataForm.changePriceEnd" placeholder="价格区间止"></el-input>
      </el-form-item>
      <el-form-item label="加价百分比" prop="chargePricePercent">
        <el-input v-model="dataForm.chargePricePercent" placeholder="加价百分比"></el-input>
      </el-form-item>
      <el-form-item label="随机百分比" prop="chargePriceRandom">
        <el-input v-model="dataForm.chargePriceRandom" placeholder="随机百分比"></el-input>
      </el-form-item>
      <el-form-item label="加价额" prop="chargePriceAmount">
        <el-input v-model="dataForm.chargePriceAmount" placeholder="加价额"></el-input>
      </el-form-item>
      <el-form-item label="最高价" prop="highestPrice">
        <el-input v-model="dataForm.highestPrice" placeholder="最高价"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        dtId: -1,
        dtName: '',
        chargePriceType: '',
        changePriceStart: 0,
        changePriceEnd: 0,
        chargePricePercent: 0,
        chargePriceRandom: 0,
        chargePriceAmount: 0,
        highestPrice: 0
      },
      dtList: [],
      dtListTreeProps: {
        label: 'name',
        children: 'children'
      },
      dataRule: {
        dtName: [
          { required: true, message: '分销商不能为空', trigger: 'blur' }
        ],
        changePriceStart: [
          { required: true, message: '价格区间始不能为空', trigger: 'blur' }
        ],
        changePriceEnd: [
          { required: true, message: '价格区间止不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      // 获取分销商下拉树
      this.$http({
        url: this.$http.adornUrl('/dt/dt/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': 1,
          'limit': 100000
        })
      }).then(({ data }) => {
        this.dtList = treeDataTranslate(data.page.list, 'id', 'parentId')
      })
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/dt/priceconfig/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.dtId = data.priceConfig.dtId
              this.dataForm.chargePriceType = data.priceConfig.chargePriceType
              this.dataForm.changePriceStart = data.priceConfig.changePriceStart
              this.dataForm.changePriceEnd = data.priceConfig.changePriceEnd
              this.dataForm.chargePricePercent = data.priceConfig.chargePricePercent
              this.dataForm.chargePriceRandom = data.priceConfig.chargePriceRandom
              this.dataForm.chargePriceAmount = data.priceConfig.chargePriceAmount
              this.dataForm.highestPrice = data.priceConfig.highestPrice
              this.dtListListTreeSetCurrentNode()
            }
          })
        }
      })
    },
    // 分销商树选中
    dtListTreeCurrentChangeHandle(data, node) {
      this.dataForm.dtId = data.id
      this.dataForm.dtName = data.name
    },
    // 分销商设置当前选中节点
    dtListListTreeSetCurrentNode() {
      this.$refs.dtTree.setCurrentKey(this.dataForm.dtId)
      this.dataForm.dtName = (this.$refs.dtTree.getCurrentNode() || {})['name']
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/dt/priceconfig/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'dtId': this.dataForm.dtId,
              'chargePriceType': this.dataForm.chargePriceType,
              'changePriceStart': this.dataForm.changePriceStart,
              'changePriceEnd': this.dataForm.changePriceEnd,
              'chargePricePercent': this.dataForm.chargePricePercent,
              'chargePriceRandom': this.dataForm.chargePriceRandom,
              'chargePriceAmount': this.dataForm.chargePriceAmount,
              'highestPrice': this.dataForm.highestPrice
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
